﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Data

Partial Class Reportes_Anexos
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Page.IsPostBack = False Then
            'cargar proyectos
            hdIdproyecto.Value = (Request.QueryString("proyecto"))
            LblTitulo.Text = (Request.QueryString("titulo"))
            Dim tipovisita As String = (Request.QueryString("visita"))
            If tipovisita = "0" Then
                hdVisita.Value = "Levantamiento de información"
            Else
                hdVisita.Value = "Visita " & tipovisita
            End If
            hdUSP.Value = (Request.QueryString("usp"))
            cargardatos()
        End If


    End Sub



    Sub cargardatos()
        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()

            Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(hdUSP.Value, cnn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@idproyecto", hdIdproyecto.Value)
                cmd.Parameters.AddWithValue("@Visita", hdVisita.Value)

                Dim reader As IDataReader = cmd.ExecuteReader()

                grdAgrupado.DataSource = reader
                grdAgrupado.DataBind()

                reader.NextResult()
                GridView1.DataSource = reader
                GridView1.DataBind()

                reader.NextResult()
                GridView2.DataSource = reader
                GridView2.DataBind()


            End Using

            cnn.Close()

        End Using


    End Sub
    Protected Sub btnExpt_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnExpt.Click

    End Sub

    Protected Sub exportarAExcel()


        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page

        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)

        'form.Controls.Add(tabla)
        form.Controls.Add(Me.grdAgrupado)
        form.Controls.Add(Me.GridView1)
        form.Controls.Add(Me.GridView2)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=Reporte.xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        cargardatos()
        exportarAExcel()
    End Sub
End Class
